//
//  AccompanySegmentControl.swift
//  HuaYun
//
//  Created by enghou on 2018/11/24.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class AccompanySegmentControl: UISegmentedControl,UIScrollViewDelegate {

    let view = UIView()
    var previousOffset: CGFloat = 0.0
    var oldIndex = 0
    var currentIdx = 0
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    override init(items: [Any]?) {
        super.init(items: items)
        self.selectedSegmentIndex = 0
        self.tintColor = UIColor.clear
        self.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)], for: .selected)
        self.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.init(red: 1, green: 1, blue: 1, alpha: 0.5),NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)], for: .normal)
        view.backgroundColor = .white
        view.tag = 1001
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 1.5
        self.addSubview(view)
        self.addTarget(self, action: #selector(changeline), for: .valueChanged)
    }
    
    @objc func changeline()->Void{
        if self.selectedSegmentIndex == 0 {
            UIView.beginAnimations("move", context: nil)
            UIView.setAnimationDuration(TimeInterval.init(0.25))
            view.left = self.width / 8
            UIView.commitAnimations()
        } else {
            UIView.beginAnimations("move", context: nil)
            UIView.setAnimationDuration(TimeInterval.init(0.25))
            view.left = self.width / 2 + self.width / 8
            UIView.commitAnimations()
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        view.frame = CGRect.init(origin: CGPoint.init(x: self.width / 8, y: self.height), size: CGSize.init(width: self.width / 4, height: 3))
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
    }

}
